﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using MotionCS.Serial;

namespace MotionCS.Helper
{
    public class CodeReaderConfi
    {
        /// <summary>
        /// 读码器启用
        /// </summary>
        public int ReaderEnable;
        /// <summary>
        /// 串口名称
        /// </summary>
        public string SerialName;
        /// <summary>
        /// 波特率
        /// </summary>
        public uint BaudRate;
        /// <summary>
        /// 数据位
        /// </summary>
        public uint DataBits;
        /// <summary>
        /// 奇偶性
        /// </summary>
        public uint Parity;
        /// <summary>
        /// 停止位
        /// </summary>
        public uint StopBits;
        /// <summary>
        /// 协议类型
        /// </summary>
        public uint ProtocolType;
        /// <summary>
        /// 触发模式
        /// </summary>
        public uint TriggerMode;
        /// <summary>
        /// 触发使能
        /// </summary>
        public string TriggerOn;
        /// <summary>
        /// 触发关闭
        /// </summary>
        public string TriggerOff;
        /// <summary>
        /// 错误字符串
        /// </summary>
        public string ErrorStr;
    }

    public class CodeReader : SerialBasic
    {
        /// <summary>
        /// 读码器配置
        /// </summary>
        private CodeReaderConfi config;
        /// <summary>
        /// 数据上传
        /// </summary>
        public Action<string> DataUpload;
        /// <summary>
        /// 二维码ID
        /// </summary>
        private static string QRCodeId = "";
        /// <summary>
        /// 条码ID
        /// </summary>
        private string codeId = "";
        /// <summary>
        /// 数据头和尾
        /// </summary>
        private char[] setx = { (char)0x02, (char)0x0d, (char)0x0a };
        /// <summary>
        /// 条码尾
        /// </summary>
        private string codeTail = "\x0d\x0a";
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CodeReader(CodeReaderConfi confi)
        {
            config = confi;
            Init();
            DataReceive += ParseQRCode;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            if (config.ReaderEnable == 0) return;

            Open(config.SerialName, (int)config.BaudRate, (int)config.Parity, (int)config.DataBits, (int)config.StopBits);
            switch (config.ProtocolType)
            {
                case 0:
                    codeTail = "\0x0d\x0a";
                    break;
                case 1:
                    codeTail = "\x0d";
                    break;
                default:
                    codeTail = "\0x0d\x0a";
                    break;
            }
        }
        /// <summary>
        /// 解析二维码数据
        /// </summary>
        /// <param name="datastr"></param>
        private void ParseQRCode(string datastr)
        {
            codeId += datastr;
            if (string.IsNullOrEmpty(config.ErrorStr) || !codeId.Contains(config.ErrorStr))
            {
                if (codeId.Contains(codeTail))
                {
                    QRCodeId = codeId.Trim(setx);
                    codeId = "";
                    DataUpload?.Invoke(QRCodeId);
                }
            }
            else
            {
                codeId = "";
                QRCodeId = "";
            }
        }
        /// <summary>
        /// 读取ID
        /// </summary>
        /// <returns></returns>
        public void GetQRCodeID(ref string codeId)
        {
            if (config.ReaderEnable == 1)
            {
                codeId = QRCodeId;
                QRCodeId = "";
            }
            else
            {
                codeId = "Disable";
            }
        }
        /// <summary>
        /// 触发打开
        /// </summary>
        public void TriggerOn()
        {
            if (!string.IsNullOrEmpty(config.TriggerOn) && config.ReaderEnable == 1 && config.TriggerMode == 1)
            {
                Logger.Logout($"读码启动命令:{config.TriggerOn}");
                SendData(config.TriggerOn + "\x0d");
            }
        }
        /// <summary>
        /// 触发关闭
        /// </summary>
        public void TriggerOff()
        {
            if (!string.IsNullOrEmpty(config.TriggerOff) && config.ReaderEnable == 1 && config.TriggerMode == 1)
            {
                SendData(config.TriggerOff +"\x0d");
            }
        }
        /// <summary>
        /// 触发设置
        /// </summary>
        /// <param name="state"></param>
        public void TriggerSet(int state)
        {
            if (state == 0)
            {
                TriggerOff();
            }

            if (state == 1)
            {
                TriggerOn();
            }
        }
    }
}
